﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Threading;
using System.Text;
using System.Diagnostics;

using Qplus.protocol;

namespace Qplus.Infrastructure
{
    public class TcpTransport : ConnectListener
    {
        PacketQueue queue;

        TCPConnection tcp_conn;

        TcpSocket socket;
        public TcpTransport(string remote_ipaddr, int remote_port, PacketQueue queue) {
            this.socket = new TcpSocket();// new TcpSocket(remote_ipaddr, remote_port);
		    this.queue = queue;
            this.socket.CreateConnection(remote_ipaddr, remote_port);
            this.socket.CreateConnectionCompleted += OnCreateConnectionCompleted;
            this.socket.MessageReceivedFromServer += OnMessageReceivedCompleted;
		    this.queue = queue;
		    //tcp_conn = new TCPConnection("tag", this, socket, this);
		    //connection_id = new ConnectionID(remote_ipaddr, remote_port);
	    }

        private void OnMessageReceivedCompleted(object sender, MessageReceivedFromServer e)
        {
            base.onReceivedData(tcp_conn, e.Message, e.Message.Length);

            //var message = Encoding.UTF8.GetString(e.Message, 0, e.Message.Length);
            //this.queue.receive();
        }

        private void OnCreateConnectionCompleted(object sender, CreateConnectionAsyncArgs e)
        {
            if (!e.ConnectionOk)
            {
                //UpdateStatus("Connection failed");
                return;
            }

            //UpdateStatus("Connection OK");

           // SendCredentialsToServer();
        }


       // private void UpdateStatus(string newStatus)
      //  {
      //      Dispatcher.BeginInvoke(() => this.ConnectionStatus.Text = newStatus);
     //   }

        public void send1(OutPacket outpk)
        {
            var mess = outpk.getData();
            this.socket.SendToServer(mess);
        }

        /**
	     * 发送数据包
	     * @param buff
	     * @param offset
	     * @param len
	     */
	    public void send(OutPacket outpk){
		    if(queue != null){
                queue.putOut(outpk);
		    }
	    }


        /**
	     * 发送数据
	     * @param buff
	     * @param offset
	     * @param len
	     * @throws IOException 
	     */
        public void send(byte[] buff, int offset, int len)
        {
            try
            {
                socket.SendToServer(buff);
		    }
            catch(Exception ex)
            {
			    throw ex;
		    }
	    }


        /**
	    * 断开连接
	    */
        public void halt()
        {
            if (socket != null)
                socket.close(2);
        }

        public void receive(InPacket inpk){
		    if(queue != null){
                queue.receive(inpk);
		    }
	}


    }
}
